﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;

namespace FicusFactor.Watermarker
{
    public partial class ConfiguratorForm : Form
    {
        private Settings settings = Settings.Load();
        private string currentImageFile;
        private bool textEntryStart;

        public bool Ready
        {
            get;
            private set;
        }

        public bool DefaultSettings
        {
            get;
            private set;
        }

        public void SetPreviewImage(string imageFile)
        {            
            currentImageFile = imageFile;
            GenerateWatermarkPreview();
        }

        private WatermarkPosition WatermarkPosition
        {
            get
            {
                return (WatermarkPosition)Enum.Parse(typeof(WatermarkPosition), positionComboBox.SelectedItem.ToString());
            }
        }

        private void ApplySettings()
        {
            if (settings.WasDeserialized == false)
            {
                DefaultSettings = true;
                settings.Transparency = 5;
                settings.FromFont(fontDialog.Font);
                settings.FromColor(colorDialog.Color = Color.White);
                settings.OutputFolder = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Desktop");
                settings.Position = WatermarkPosition.Center;
                settings.WatermarkText = watermarkTextBox.Text;
            }
            else
            {
                fontDialog.Font = settings.ToFont();
                colorDialog.Color = settings.ToColor();
            }

            watermarkTextBox.Text = settings.WatermarkText;
            positionComboBox.SelectedIndex = (int)settings.Position;
            transparencyTrackBar.Value = settings.Transparency;
            outputFolderTextBox.Text = settings.OutputFolder;
            SetFontTextbox();
            colorPictureBox.BackColor = colorDialog.Color;
        }

        public ConfiguratorForm()
        {
            InitializeComponent();
            positionComboBox.Items.Add(WatermarkPosition.Center);
            positionComboBox.Items.Add(WatermarkPosition.Top);
            positionComboBox.Items.Add(WatermarkPosition.Bottom);
            positionComboBox.Items.Add(WatermarkPosition.Diagonal);

            ApplySettings();
        }

        private void GenerateWatermarkPreview()
        {
            if (previewPictureBox.Image != null)
            {
                previewPictureBox.Image.Dispose();
            }

            previewPictureBox.Image = new Bitmap(Watermarker.ApplyWatermark(settings, currentImageFile), previewPictureBox.Size);
        }

        private void SetFontTextbox()
        {
            fontTextBox.Text = string.Format("{0} ({1})", fontDialog.Font.Name, fontDialog.Font.Size);
        }

        private void OnFontSelectButtonClick(object sender, EventArgs e)
        {
            DialogResult result = fontDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                settings.FromFont(fontDialog.Font);
                SetFontTextbox();
                settings.Save();
                GenerateWatermarkPreview();
            }
        }

        private void OnColorSelectButtonClick(object sender, EventArgs e)
        {
            DialogResult result = colorDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                settings.FromColor(colorPictureBox.BackColor = colorDialog.Color);
                settings.Save();
                GenerateWatermarkPreview();
            }
        }

        private void OnPositionComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            if (settings.Position != (WatermarkPosition)positionComboBox.SelectedIndex)
            {
                settings.Position = (WatermarkPosition)positionComboBox.SelectedIndex;
                settings.Save();
                GenerateWatermarkPreview();
            }
        }

        private void OnOkButtonClick(object sender, EventArgs e)
        {
            settings.Save();
            Close();
        }

        private void OnTransparencyTrackBarScroll(object sender, EventArgs e)
        {
            settings.Transparency = transparencyTrackBar.Value;
            settings.Save();
            GenerateWatermarkPreview();
        }

        private void OnCancelButtonClick(object sender, EventArgs e)
        {
            Close();
        }

        private void OnOutputFolderButtonClick(object sender, EventArgs e)
        {
            if (outputFolderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                settings.OutputFolder = outputFolderTextBox.Text = outputFolderBrowserDialog.SelectedPath;
                settings.Save();
            }
        }

        private void OnTextEntryPreviewEvent(object sender, EventArgs e)
        {
            if (true == textEntryStart)
            {
                settings.WatermarkText = watermarkTextBox.Text;
                settings.Save();
                textEntryStart = false;
                GenerateWatermarkPreview();
            }
        }

        private void OnWatermarkTextBoxMouseClick(object sender, MouseEventArgs e)
        {
            if (true == textEntryStart)
            {
                settings.WatermarkText = watermarkTextBox.Text;
                settings.Save();
                textEntryStart = false;
                GenerateWatermarkPreview();
            }
        }

        private void OnWatermarkTextBoxTextChanged(object sender, EventArgs e)
        {
            settings.WatermarkText = watermarkTextBox.Text;
            textEntryStart = true;
        }

        private void OnFormClosing(object sender, FormClosingEventArgs e)
        {
            settings.Save();
        }

        private void OnProportionalFontSizeCheckedChanged(object sender, EventArgs e)
        {
            settings.FontSizeIsProportional = proportionalTextBox.Checked;
            settings.Save();
            GenerateWatermarkPreview();
        }
    }
}
